package com.dunqi.gpm.shunqing.home.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseFragment;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.RecyclerItemDecoration;
import com.dunqi.gpm.shunqing.home.activity.AuditDeatilActivity;
import com.dunqi.gpm.shunqing.home.adapter.ApprovalReviewAdapter;
import com.dunqi.gpm.shunqing.home.bean.EarlyInfo;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.constance.Constance;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/11/26.
 */

public class ApprovalReviewFragment extends BaseFragment implements AdapterView.OnItemSelectedListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;

    List<EarlyInfo> data = new ArrayList();

    HashMap<String, String> parms = new HashMap<>();

    private ApprovalReviewAdapter adapter;

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }


    private void net() {
        parms.put(Constance.PAGE, String.valueOf(page));
        parms.put("stage_resp_org_id", String.valueOf(UserUtil.getOrgId()));
        parms.put("stage_status", "1");

        ApiFactory.getInstance()
                .queryProjectPreStageList(parms)
                .compose(RxSchedulers.<Page<EarlyInfo>>io_main())
                .subscribe(new Consumer<Page<EarlyInfo>>() {
                    @Override
                    public void accept(Page<EarlyInfo> earlyInfoPage) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);

                        ArrayList<EarlyInfo> list = earlyInfoPage.getList();

                        if (page == 1) {
                            data.clear();
                        }

                        data.addAll(list);
                        page++;
                        adapter.notifyDataSetChanged();

                        if (earlyInfoPage.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd();
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                    }
                });
    }

    @Override
    protected void initUiAndListener() {

        spinner.setOnItemSelectedListener(this);
        recycler.setLayoutManager(new LinearLayoutManager(context));
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));

        adapter = new ApprovalReviewAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(context));
        recycler.addItemDecoration(new RecyclerItemDecoration(1, RecyclerItemDecoration.VERTICAL));
        adapter.setOnLoadMoreListener(this, recycler);
        swipeRefresh.setOnRefreshListener(this);

        adapter.setEmptyView(new EmptyView(context));
        adapter.setOnItemClickListener(this);

    }

    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        refreshData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_early_info;
    }

    public static ApprovalReviewFragment newInstance() {
        ApprovalReviewFragment fragment = new ApprovalReviewFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //EditRequestActivity
        if (resultCode == 400) {
            refreshData();
        }
        //AddEarlyActivity
        if (resultCode == 500) {
            refreshData();
        }

        //CompleteEarlyInfoActivity
        if (resultCode == 365) {
            refreshData();
        }


    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        refreshData();
    }

    private void refreshData() {
        page = 1;
        net();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    //    0 创建
//1 提交审批申请(实际启动)
//2 完成审批(实际完成)
    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        EarlyInfo item = data.get(position);
        AuditDeatilActivity.start((Activity) context,item);



    }
}
